package com.xkdx.guangguang.fragment.comment;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.xkdx.caipiao.R;
import com.xkdx.guangguang.ManagerActivity;
import com.xkdx.guangguang.adapter.ShopCommentAdapter;
import com.xkdx.guangguang.adapter.ShopMoreCommentAdapter;
import com.xkdx.guangguang.fragment.BaseFragment;
import com.xkdx.guangguang.module.network.AbsModule;
import com.xkdx.guangguang.module.statics.IConstants;
import com.xkdx.guangguang.module.util.ErrorModule;
import com.xkdx.guangguang.shareclass.DaZhongDetailBean;
import com.xkdx.guangguang.shareclass.Shop;
import com.xkdx.guangguang.shareclass.ShopCommentary;
import com.xkdx.guangguang.util.SharePrefenceUtil;
import com.xkdx.guangguang.wiget.XListView;
import com.xkdx.guangguang.wiget.XListView.IXListViewListener;

public class ShopMoreCommentFragment extends BaseFragment {
	View view;
	LayoutInflater inflater;
	int PageIndex = 1;
	String shopID, ShopName, UserID, LoginToken, LoadMoreFlag, brandID;
	List<Shop> list = null;
	private Button btn_back;
	private Button btn_commit;
	List<ShopCommentary> shopCommentaryList;
	List<ShopCommentary> infos;
	List<DaZhongDetailBean> daZhongList = new ArrayList<DaZhongDetailBean>();
	ShopCommentAdapter mShopCommAdapter;
	XListView shop_maincommentary;
	String num;
	SharePrefenceUtil su;
	int pageIndex = 1;
	private TextView tv_showCounts;
	public static boolean isLoadMore = false;
	ImageLoader imageLoader = ImageLoader.getInstance();
	DisplayImageOptions options;
	ShopMoreCommentAdapter shopCommentAdapter;
	ShopMoreCommentAction shopMoreCommentAction;
	ShopMoreCommentModule shopMoreCommentModule;
	ShopMoreCommnentPresistence shopMoreCommnentPresistence;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		shopID = getArguments().getString("ShopID");
		ShopName = getArguments().getString("ShopName");
		SharePrefenceUtil su = new SharePrefenceUtil(getActivity(),
				IConstants.SP_USER);
		UserID = su.getUserID(); // 用户登录id,必须保证用户处于登录状态
		LoginToken = su.getUserLoginToken();// 登录token
		inflater = LayoutInflater.from(getActivity());

		// 新增直接获取
		shopCommentaryList = new ArrayList<ShopCommentary>();
		infos = new ArrayList<ShopCommentary>();

		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.shop_empty)
				.showImageForEmptyUri(R.drawable.shop_empty)
				.showImageOnFail(R.drawable.shop_empty)
				.resetViewBeforeLoading().cacheOnDisc()
				.imageScaleType(ImageScaleType.EXACTLY)
				.bitmapConfig(Bitmap.Config.RGB_565).build();

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		view = inflater.inflate(R.layout.shop_commmentarymoreinfos, container,
				false);
		findView();
		setOnClick();
		showLoading();
		return view;
	}

	@Override
	public void findView() {
		IConstants.hideInputMethodAways(getActivity(), view);
		btn_back = (Button) view.findViewById(R.id.btn_morecommentback);
		btn_commit = (Button) view.findViewById(R.id.btn_commentadd);
		tv_showCounts = (TextView) view.findViewById(R.id.tv_showcommentcounts);
		shop_maincommentary = (XListView) view
				.findViewById(R.id.strollshopcommentary_list);
		shop_maincommentary.setPullLoadEnable(true);
		shopMoreCommentModule = new ShopMoreCommentModule();

	}

	@Override
	public void setOnClick() {
		btn_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				getFragmentManager().popBackStack();
			}
		});
		btn_commit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				SharePrefenceUtil su = new SharePrefenceUtil(getActivity(),
						IConstants.SP_USER);
				String loginToken = su.getUserLoginToken();
				if (!loginToken.equals("")) {
					ShopCommentFragment shopCommentFragment = new ShopCommentFragment();
					Bundle b = new Bundle();
					b.putString("ShopID", shopID);
					b.putString("ShopName", ShopName);
					shopCommentFragment.setArguments(b);
					getFragmentManager()
							.beginTransaction()
							.replace(R.id.fragment, shopCommentFragment,
									"shopCommentaryFragment")
							.addToBackStack(null).commit();

				} else {
					UserLogin();
				}

			}
		});

	}

	@Override
	public void showLoading() {
		dataLoadDialog.show();
		pageIndex = 1;


		shopMoreCommentAction = new ShopMoreCommentAction(shopID, IConstants.getCommentTime(),
				String.valueOf(pageIndex));

		shopMoreCommnentPresistence = new ShopMoreCommnentPresistence(this);
		((ManagerActivity) getActivity())
				.addCurrentTask(shopMoreCommnentPresistence);
		shopMoreCommnentPresistence.setActitons(shopMoreCommentAction);
		shopMoreCommnentPresistence.setModule(shopMoreCommentModule);
		shopMoreCommnentPresistence.execute();
		dataLoadDialog.setOnCancelListener(new OnCancelListener() {

			@Override
			public void onCancel(DialogInterface dialog) {
				if (shopMoreCommnentPresistence != null) {
					shopMoreCommnentPresistence.cancel(true);
				}
				getFragmentManager().popBackStack();

			}
		});
		dataLoadDialog.show();
	}

	@Override
	public void showLoaded(HashMap<String, AbsModule> map) {
		dataLoadDialog.dismiss();
		if (map.containsKey("0")) {
			shopCommentaryList = shopMoreCommentModule.shopCommentList;
			if (shopCommentaryList.size() != 0) {
				shopCommentAdapter = new ShopMoreCommentAdapter(getActivity(),
						shopCommentaryList, imageLoader, shopID, ShopName);
				shop_maincommentary.setAdapter(shopCommentAdapter);
				shop_maincommentary.setXListViewListener(xlistner);
				tv_showCounts.setText(shopMoreCommentModule.shopCommentNum
						+ "条评论");
			}
		} else {
			refresh(map);
		}
		onLoad();
	}

	// 加载更多
	private void loadMore() {
		if (isLoadMore) {
			return;
		}
		pageIndex++;
		isLoadMore = true;
		shopMoreCommentAction = new ShopMoreCommentAction(shopID,
				IConstants.getCurrentTime(), String.valueOf(pageIndex));
		shopMoreCommnentPresistence = new ShopMoreCommnentPresistence(this);
		((ManagerActivity) getActivity())
				.addCurrentTask(shopMoreCommnentPresistence);
		shopMoreCommnentPresistence.setActitons(shopMoreCommentAction);
		shopMoreCommnentPresistence.setModule(shopMoreCommentModule);
		shopMoreCommnentPresistence.execute();

	}

	@Override
	public void showClick(HashMap<String, AbsModule> map) {
	}

	@Override
	public void showLoadMore(HashMap<String, AbsModule> map) {
		isLoadMore = false;
		if (map.containsKey("0")) {
			if (shopMoreCommentModule.shopCommentList.size() != 0) {
				for (ShopCommentary shopCommentary : shopMoreCommentModule.shopCommentList) {
					shopCommentaryList.add(shopCommentary);
				}
				shopCommentAdapter.notifyDataSetChanged();
				tv_showCounts.setText(shopMoreCommentModule.shopCommentNum
						+ "条评论");
			}
		} else {
			Toast.makeText(getActivity(),
					((ErrorModule) map.get("1")).errorMessage,
					Toast.LENGTH_SHORT).show();
		}
		onLoad();
	}

	IXListViewListener xlistner = new IXListViewListener() {

		@Override
		public void onRefresh() {
			showLoading();

		}

		@Override
		public void onLoadMore() {
			loadMore();

		}
	};

	private void onLoad() {
		shop_maincommentary.stopRefresh(); // 下拉刷新
		shop_maincommentary.stopLoadMore();
		shop_maincommentary.setRefreshTime(getCurrentTime());
	}
	private String getCurrentTime() {
		SimpleDateFormat format = new SimpleDateFormat("MM-dd hh:mm");
		Date date = new Date(System.currentTimeMillis());
		return format.format(date);
	}
}
